package com.test.burnincash.aaahpoppomelo.activity;

import android.app.AlertDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;

import com.test.burnincash.R;
import com.test.burnincash.aaahpoppomelo.base.BaseActivity_hpoppomelo;
import com.test.burnincash.aaahpoppomelo.bean.Order_hpoppomelo;
import com.test.burnincash.aaahpoppomelo.bean.UserData_hpoppomelo;
import com.test.burnincash.util_burnin.StringUtil_burnin;

public class EditOrderActivity_hpoppomelo extends BaseActivity_hpoppomelo {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_order_hpoppomelo);
        init();
    }

    private void init(){

        setBackBtn_burnin(findViewById(R.id.activity_edit_order_ib_back));

        EditText orderNumberEt = findViewById(R.id.activity_edit_order_et_order_number);
        TextView dateTv = findViewById(R.id.activity_edit_order_tv_date);
        EditText customerEt = findViewById(R.id.activity_edit_order_et_customer);

        orderNumberEt.setText(String.valueOf(Order_hpoppomelo.optionOrder.getOrderNumber()));
        dateTv.setText(Order_hpoppomelo.optionOrder.getDate());
        customerEt.setText(Order_hpoppomelo.optionOrder.getCustomer());

        dateTv.setOnClickListener(v -> {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            View dialogView = getLayoutInflater().inflate(R.layout.dialog_date_picker_burnin,null);
            DatePicker datePicker = dialogView.findViewById(R.id.dialog_date_picker_view);
            datePicker.setCalendarViewShown(false);
            builder.setView(dialogView).setTitle(getResources().getString(R.string.dialog_date_picker_title));
            builder.setPositiveButton(getResources().getString(R.string.word_submit),(dialog,which) -> {
                int month = datePicker.getMonth() + 1;
                int day = datePicker.getDayOfMonth();
                int year = datePicker.getYear();
                String monthStr = month < 10 ? "0" + month : String.valueOf(month);
                String dayStr = day < 10 ? "0" + day : String.valueOf(day);
                String input = dayStr + "/" + monthStr + "/" + year;
                dateTv.setText(input);
                dialog.cancel();
            });
            builder.setNegativeButton(getResources().getString(R.string.word_cancel), ((dialog, which) -> dialog.cancel()));
            builder.show();
        });

        findViewById(R.id.activity_edit_order_btn_confirm).setOnClickListener(v1 -> {
            if (!TextUtils.isEmpty(orderNumberEt.getText())) Order_hpoppomelo.optionOrder.setOrderNumber(StringUtil_burnin.safeParseInt(orderNumberEt.getText().toString()));
            if (!TextUtils.isEmpty(dateTv.getText())) Order_hpoppomelo.optionOrder.setDate(dateTv.getText().toString());
            if (!TextUtils.isEmpty(customerEt.getText())) Order_hpoppomelo.optionOrder.setCustomer(customerEt.getText().toString());

            UserData_hpoppomelo.getInstance().uploadData();
            finish();
        });

    }
}